Ext.define('SA.controller.Survey', {
	extend: 'Ext.app.Controller',
	models: ['Survey', 'Question', 'Language', 'QuestionType','SurveyStatus'],
	stores: ['Surveys', 'Questions', 'Languages', 'QuestionTypes','SurveyStatuses'],

	views: ['survey.Editor', 'survey.QuestionPanel', 'survey.QuestionDataView', 'survey.QuestionEditor'],
	init: function() {
		this.getStore('Surveys').on('load', this.loadSurveyDetails, this, {
			single: true, 
			delay: 100
		});
		var queryParameters = 
			Ext.urlDecode(window.location.search.substring(1));
		
		this.getStore('Questions').on('load', this.loadQuestionDetails, this, {
			single: true,
			delay: 100
		});
		this.getStore('Questions').load({
			params: {
				surveyId: queryParameters.id
			}
		});
		
		this.control({
			'surveyeditor': {
				afterrender: function (self, options) {
					
					console.log('Editor has been rendered.' + self.id);
//					var surveyStore = this.getStore('Surveys');
//					var queryParameters = 
//						Ext.urlDecode(window.location.search.substring(1));
//					var editor = this.getView('survey.Edit');
//					var record = 
//						surveyStore.findRecord('id', queryParameters.id, true);
//					console.log(new Date().getTime() + ' lookup of ' + queryParameters.id + ' results in ' + record);
					//self.loadRecord();
				}
		
			},
			'surveyeditor button[action=save]': {
				click: this.updateSurvey
			}, 
			'questionpanel questionDataView': {
				itemdblclick: this.editQuestion
			}
		
		});
		console.log('Survey controller init() called.');
		
	},

	loadSurveyDetails: function(self) {
		console.log('loadSurveyDetails() fired');
		var queryParameters = 
			Ext.urlDecode(window.location.search.substring(1));
	
		var editor = Ext.ComponentQuery.query('surveyeditor')[0];
		var record = 
			self.findRecord('id', queryParameters.id);
		if (!record) {
			
		} else {
			editor.loadRecord(record);
			
		}
		console.log('loadSurveyDetails completed');

	} ,
	
	loadQuestionDetails: function (self) {
		console.log('loadQuestionDetails() fired');
		self.filter([{
			property: 'languageId', value: 1
		}]);
//		var question = Ext.ComponentQuery.query('questiontabpael')[0];
//		var queryParameters = 
//			Ext.urlDecode(window.location.search.substring(1));
//		var languageStore = this.getStore('Languages');
//		var matchingQuestions = self.queryBy(function(record) {
//			if (record.get('questionId') == 1) {
//				console.log('Loading question data for language ' + record.get('languageId'));
//				var language = languageStore.findRecord('id', record.get('languageId'));
//				
//				var questionForm = Ext.widget('questionform');
//				questionForm.setTitle(language.get('name'));
//				questionForm.loadRecord(record);
//				
//				var questionTabPanel = Ext.ComponentQuery.query('questiontabpanel')[0];
//				questionTabPanel.add(questionForm);
//				return true;
//			}
//			return false;
//			
//		});
//		
//		Ext.each(matchingQuestions, function(item, index) {
//			if (!item) {
//				console.log('A matching question is undefined.');
//			} else {
//				console.log('Loading question data for language ' + item.get('languageId'));
//				var questionForm = Ext.widget('questionform');
//				questionForm.loadRecord(item);
//				var questionTabPanel = Ext.ComponentQuery.query('questiontabpanel')[0];
//				questionTabPanel.add(questionForm);
//				
//			}
//			 
//		});
		
		console.log('loadQuestionDetails completed');
	},
	
	updateSurvey: function(button) {
		console.log('Updating survey');
		var form = button.up('form');
		var record = form.getRecord();
		var values = form.getValues();
		record.set(values);
		
	},
	
	editQuestion: function(self, record, item, index, event, options) {
		console.log('Record ' + record.get('questionId') + ':' + record.get('languageId') + ' clicked');
		var questionEditor = Ext.widget('questioneditor');
		questionEditor.down('form').loadRecord(record);
		
	}
	
});